package name.matthewgreet.strutscommons.interceptor; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import name.matthewgreet.strutscommons.form.FormattableForm; import name.matthewgreet.strutscommons.util.DefaultFormFormatter; import name.matthewgreet.strutscommons.util.DefaultPolicyLookup; import name.matthewgreet.strutscommons.util.FormFormatter; import name.matthewgreet.strutscommons.util.PolicyLookup; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; import com.opensymphony.xwork2.interceptor.PreResultListener; /** * <P>Struts 2 Interceptor used by view Actions for writing formatted versions of forms onto the Value Stack, so they're * displayed instead of the unformatted form, using any conversion annotations on form fields. Such forms must * implement {@link FormattableForm}. Does not apply to injected forms as its conversion errors are copied.</P> * * <P><U>Interceptor parameters:</U></P> * <DL> * <DT>disabled</DT> * <DD>If true, all processing for this action is disabled. Defaults to false.</DD> * </DL> * * <P><U>Extending the interceptor:</U></P> * <P>The following methods can be overridden :-</P> * <DL> * <DT>makeFormFormatLibrary</DT><DD>Creates helper that formats form.</DD> * <DT>makePolicyLookup</DT><DD>Creates helper that finds policy implementation from the annotation that configures it.</DD> * </DL> * * <P> <u>Example code:</u></P> * <PRE> * <action name="someAction" class="com.examples.SomeAction"> * <interceptor-ref name="annotationValidation"/> * <interceptor-ref name="validation"/> * <interceptor-ref name="formFormatter"/> * <result name="success">good_result.ftl</result> * </action> * <!-- END SNIPPET: example --> * </PRE> */ @SuppressWarnings("deprecation") public class FormFormatterInterceptor extends AbstractInterceptor { public class FormFormatterPreResultListener implements PreResultListener { public void beforeResult(ActionInvocation invocation, String resultCode) { FormFormatter formFormatLibrary; PolicyLookup policyLookup; policyLookup = makePolicyLookup(); formFormatLibrary = makeFormFormatLibrary(); formFormatLibrary.formatForms(policyLookup); } } private static final long serialVersionUID = 2689404505465349761L; private boolean disabled = false; @SuppressWarnings("unused") private Logger LOG = LogManager.getLogger(FormFormatterInterceptor.class); /** * Creates and returns helper that formats form field values. */ protected FormFormatter makeFormFormatLibrary() { return new DefaultFormFormatter(); } /** * Creates and returns helper that finds a policy implementation from a form field adjuster, converter, or validator * annotation. Does not scan for non-built-in, non-custom policies if disabledClassScanner is true. */ protected PolicyLookup makePolicyLookup() { return DefaultPolicyLookup.getInstance(); } public boolean getDisabled() { return disabled; } public void setDisabled(boolean disabled) { this.disabled = disabled; } @Override public String intercept(ActionInvocation invocation) throws Exception { if (!disabled) { invocation.addPreResultListener(new FormFormatterPreResultListener()); } return invocation.invoke(); } }